package com.anolesoft.amljmes.foreknow.controller;

import com.anolesoft.amljmes.foreknow.bo.MesBzzPtd;
import com.anolesoft.amljmes.foreknow.bo.MesBzzPtdmx;
import com.anolesoft.amljmes.foreknow.bo.MesBzzPtjh;
import com.anolesoft.amljmes.foreknow.bo.MesBzzPtjhmx;
import com.anolesoft.amljmes.foreknow.dto.MesBzzPtjhDto;
import com.anolesoft.amljmes.foreknow.dto.WebDto;
import com.anolesoft.amljmes.foreknow.mapper.MesBzzPtjhMapper;
import com.anolesoft.amljmes.foreknow.service.MesBzzPtjhService;
import com.anolesoft.amljmes.foreknow.service.MesBzzPtjhmxService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/mesbzzptjh")
public class MesBzzPtjhController {
    @Autowired
    private MesBzzPtjhService service;

    @Autowired
    private MesBzzPtjhmxService ptjhmxService;
    @Autowired
    private MesBzzPtjhMapper mapper;


    @RequestMapping(value = "/query" , method = RequestMethod.POST)
    public AjaxResult queryMesBzzPtjhInfo(@RequestBody MesBzzPtjhDto request){
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String fcode = loginUser.getFacCode();
        MesBzzPtjhDto dto = new MesBzzPtjhDto();
        dto.setFactorycode(fcode);
        Integer pageNum = request.getPageNum();
        Integer pageSize = request.getPageSize();
        if (request.getOrdernumber().trim() != null){
            dto.setOrdernumber(request.getOrdernumber().trim());
        }
        if (request.getPartdispatchnumber().trim() != null){
            dto.setPartdispatchnumber(request.getPartdispatchnumber().trim());
        }
        if (request.getModel().trim() != null){
            //小写转大写
            dto.setModel(request.getModel().trim().toUpperCase());
        }
        if (request.getDrawingno().trim() != null){
            //小写转大写
            dto.setDrawingno(request.getDrawingno().trim().toUpperCase());
        }
        if (request.getCustomer().trim() != null){
            dto.setCustomer(request.getCustomer().trim());
        }
        if (request.getItemno().trim() != null){
            dto.setItemno(request.getItemno().trim());
        }
        if (request.getBomclassification().trim() != null){
            dto.setBomclassification(request.getBomclassification().trim());
        }
        if (request.getDispatchnoofgeneral().trim() != null){
            dto.setDispatchnoofgeneral(request.getDispatchnoofgeneral().trim());
        }
        if (request.getProducttype().trim() != null){
            dto.setProducttype(request.getProducttype().trim());
        }
        List<MesBzzPtjh> allList = service.queryAll(dto);
        int endIndex = pageNum * pageSize > allList.size() ? allList.size() : pageNum * pageSize;
        List<MesBzzPtjh> list = allList.subList( (pageNum-1) *pageSize, endIndex);
        PageResult pageResult = new PageResult(list, pageSize, pageNum, allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }
//    @RequestMapping(value = "/update" , method = RequestMethod.POST)
//    public AjaxResult updateDelete(@RequestBody List<String> list){
//        int a = service.update(list);
//        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,a);
//    }

    @Transactional
    @RequestMapping(value = "/insert" , method = RequestMethod.POST)
    public AjaxResult insertPtd(@RequestBody WebDto dto) throws ParseException {
        int num = 0;
        String uid = "";
        String fid = "";
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        String fcode = loginUser.getFacCode();
        List<String> list = dto.getErpids();
        List<String> objList = dto.getObjids();
        int c = service.update(objList);
        for (String id:list) {
            MesBzzPtjh linshi = new MesBzzPtjh();
            linshi.setIerpobjuid(id);
            linshi.setFactorycode(fcode);
            MesBzzPtjh mesBzzPtjh = service.ptjh(linshi);
            //插入配套单
            MesBzzPtd vo = new MesBzzPtd();
            vo.setObjuid(vo.getObjuid());
            vo.setIsItComplete(0);
            vo.setState(0);
            String a = "PTD";
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String dateNow = sdf.format(date);
            int shui = mapper.liuShuiHao();
            String COUNT = shui + "";
            String ptdh = a + dateNow + COUNT;
            vo.setSupportingOrderNo(ptdh);
            if (mesBzzPtjh.getOrdernumber() != null){
                vo.setOrderNumber(mesBzzPtjh.getOrdernumber());
            }
            if (mesBzzPtjh.getBatch() != null){
                vo.setBatch(mesBzzPtjh.getBatch());
            }
            if (mesBzzPtjh.getPartdispatchnumber() != null){
                vo.setPartDispatchNumber(mesBzzPtjh.getPartdispatchnumber());
            }
            if (mesBzzPtjh.getModel() != null){
                vo.setModel(mesBzzPtjh.getModel());
            }
            if (mesBzzPtjh.getBomclassification() != null){
                vo.setBomClassification(mesBzzPtjh.getBomclassification());
            }
            if (mesBzzPtjh.getSorties() != null){
                vo.setSorties(mesBzzPtjh.getSorties());
            }
            if (mesBzzPtjh.getItemno() != null){
                vo.setItemNo(mesBzzPtjh.getItemno());
            }
            if (mesBzzPtjh.getMaterialname() != null){
                vo.setMaterialName(mesBzzPtjh.getMaterialname());
            }
            if (mesBzzPtjh.getDrawingno() != null){
                vo.setDrawingNo(mesBzzPtjh.getDrawingno());
            }
            if (mesBzzPtjh.getQuantity() != null){
                vo.setQuantity(mesBzzPtjh.getQuantity());
            }
            if (mesBzzPtjh.getDemandtime() != null){
                vo.setDemandTime(mesBzzPtjh.getDemandtime());
            }
            if (mesBzzPtjh.getDispatchnoofgeneral() != null){
                vo.setDispatch(mesBzzPtjh.getDispatchnoofgeneral());
            }
            if (mesBzzPtjh.getCustomer() != null){
                vo.setCustomer(mesBzzPtjh.getCustomer());
            }
            if (mesBzzPtjh.getProducttype() != null){
                vo.setProductType(mesBzzPtjh.getProducttype());
            }
            if (mesBzzPtjh.getFactorycode() != null){
                //厂代号
                vo.setFactoryCode(mesBzzPtjh.getFactorycode());
                fid = vo.getFactoryCode();
            }
            String objuid = loginUser.getEmpUid();
                vo.setFounder(objuid);

            String timeStr= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date aa = ft.parse(timeStr);
            vo.setCreationTime(aa);
            if (mesBzzPtjh.getDeletetag() != null){
                vo.setDeleteTag(1);
            }
            if (mesBzzPtjh.getDeletetime() != null){
                vo.setDeleteTime(mesBzzPtjh.getDeletetime());
            }
            if (mesBzzPtjh.getObjuid() != null){
                vo.setSupportingPlanPk(mesBzzPtjh.getObjuid());
            }

            int aaa = service.insertData(vo);
            //ptd主键
            uid = vo.getObjuid();

            if (aaa > 0){
                num++;
            }
            //插入配套单明细
            List<MesBzzPtjhmx> listmx = ptjhmxService.queryAll(id);
            if (listmx.size() != 0){
                for (MesBzzPtjhmx mx:listmx) {
                    MesBzzPtdmx mesBzzPtdmx = new MesBzzPtdmx();
                    if(mx.getItemno() != null){
                        mesBzzPtdmx.setItemNo(mx.getItemno());
                    }
                    if (mx.getPartcode() != null){
                        mesBzzPtdmx.setPartCode(mx.getPartcode());
                    }
                    if (mx.getDrawingno() != null){
                        mesBzzPtdmx.setPartDrawingNo(mx.getDrawingno());
                    }
                    if (mx.getDrawingnoofstation() != null){
                        mesBzzPtdmx.setDrawingNoOfStation(mx.getDrawingnoofstation());
                    }
                    if (mx.getStationname() != null){
                        mesBzzPtdmx.setStationName(mx.getStationname());
                    }
                    if (mx.getMaterialtype() != null){
                        mesBzzPtdmx.setMaterialType(mx.getMaterialtype());
                    }
                    if (mx.getManufacturingunit() != null){
                        mesBzzPtdmx.setManufacturingUnit(mx.getManufacturingunit());
                    }
                    if (mx.getUserunit() != null){
                        mesBzzPtdmx.setUserUnit(mx.getUserunit());
                    }
                    if (mx.getProductsource() != null){
                        mesBzzPtdmx.setProductSource(mx.getProductsource());
                    }
                    if(mx.getDemandtime() != null){
                        mesBzzPtdmx.setDemandTime(mx.getDemandtime());
                    }
                    if (mx.getDemandquantity() != null){
                        mesBzzPtdmx.setDemandQuantity(mx.getDemandquantity());
                    }
                    mesBzzPtdmx.setMatchedQuantity(0);
                    if (mx.getDemandquantity() != null){
                        mesBzzPtdmx.setQuantityOfMissingParts(mx.getDemandquantity());
                    }
                    if (mx.getObjuid() != null){
                        mesBzzPtdmx.setDetailsPk(mx.getObjuid());
                    }
                    mesBzzPtdmx.setMatchingDocPk(uid);
                    mesBzzPtdmx.setFactoryCode(fid);
                    if (mx.getDispatchnumber() != null){
                        mesBzzPtdmx.setPaigonghao(mx.getDispatchnumber());
                    }
                    if (mx.getModel() != null){
                        mesBzzPtdmx.setJixing(mx.getModel());
                    }
                    if (mx.getDeliveryworkshop() != null){
                        mesBzzPtdmx.setJiaofuchejian(mx.getDeliveryworkshop());
                    }
                    if (mx.getReceivingworkshop() != null){
                        mesBzzPtdmx.setJieshouchejian(mx.getReceivingworkshop());
                    }
                    if (mx.getDeliverysequence() != null){
                        mesBzzPtdmx.setJiaofushunxu(mx.getDeliverysequence());
                    }
                    ptjhmxService.insertData(mesBzzPtdmx);
                }
            }
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,num);
    }
}
